package week_eight;

public class LongestCommonPrefix_14 {
    public String longestCommonPrefix(String[] strs) {
        int n = strs.length;

        String res = strs[0];
        for (int i = 1; i < n; i++) {
            res = getCommonPrefix(res, strs[i]);
        }
        return res;

    }

    private String getCommonPrefix(String res, String next) {
        int i = 0;
        while (i < res.length() && i < next.length()) {
            if (res.charAt(i) != next.charAt(i)) {
                break;
            }
            i++;
        }
        return res.substring(0, i);
    }
}
